home *** CD-ROM | disk | FTP | other *** search
/ s-gikan2.maizuru-ct.ac.jp / s-gikan2.maizuru-ct.ac.jp.zip / s-gikan2.maizuru-ct.ac.jp / pub / ncvc352a_install.exe / {app} / scripts / transpose_laser_Gcode.pl < prev    next >
Perl Script  |  2007-10-23  |  5KB  |  222 lines

  1. #! /usr/bin/perl
  2.  
  3. #  âîü[âUü[ë┴ìHï@ùpé╠GâRü[âhé≡ò╧è╖é╖éΘâXâNâèâvâg  #
  4. #  (G08, G11.1, G11 é¬æ╬Å█)                              #
  5. #  ÉΓæ╬ì└òWîn(G90)é╠é▌æ╬ë₧                                  #
  6.  
  7.  
  8. use Math::Trig;
  9. $EPSILON = 0.0001;
  10.  
  11. $pre_file= $ARGV[0];
  12. $out_file= $ARGV[1];
  13.  
  14. open(IN,$pre_file);
  15. @line = <IN>;
  16. close(IN);
  17.  
  18. open(OUT,">$out_file");
  19.  
  20. my ($line,$i);
  21. my ($G0X,$radius);
  22. my $word;
  23. my $commentLine;
  24.  
  25. my ($x,$y);    #ê┌ô«æOé╠ì└òWé≡èiö[
  26. my @abso;    #ê┌ô«îπé╠ì└òW
  27. my @pass;    #Æ╩ë▀ô_é╠ì└òW
  28. my @destination;    #ê┌ô«îπé╠ì└òW
  29. my @center;    #ë~î╩é╠ÆåÉSì└òW
  30. my @passAx;    #ë~î╩òΓè╘Ä₧é╔é▄é╜é«Ä▓é╠âèâXâg
  31.  
  32. %XYZ = ("X",0,"Y",1,"Z",2,"I",0,"J",1);
  33.  
  34. for($i=0;$i<=$#line;$i++){
  35.     $_ = $line[$i];
  36.     
  37.     if(/^\s*[\%]/){ print OUT; }
  38.     else{
  39.         ($_, $commentLine) = divideComment($_);
  40.         
  41.         if(/G0*?([018])[A-Z\s]/){ $G0X = $1; }
  42.         elsif(/G11[A-Z\s]/){ $G0X = 11; }
  43.         elsif(/G11.1[A-Z\s]/){ $G0X = 11.1; }
  44.         
  45.         if($G0X == 8 and /D([^A-Z\s]+)/){
  46.             
  47.             $radius = marume($1/2);
  48.             
  49.             #î┤ò╢é≡âRâüâôâgÅoù═
  50.             outputOriginal($line[$i]);
  51.             
  52.             #G08,D_,K_ê╚èO+âRâüâôâgé≡Åoù═
  53.             while(/[A-CE-JL-Z][^A-Z\s]+/g){
  54.                 $word = $&;
  55.                 if($word !~ /G0*8/){
  56.                     print OUT $word;
  57.                 }
  58.             }
  59.             print OUT $commentLine."\n";
  60.             
  61.             $startX = marume($abso[0] + $radius);
  62.             $abso[0] = marume($abso[0] + $startX);
  63.             print OUT "G00X".$startX."\n";
  64.             print OUT "G03I-".$radius."\n";
  65.         }
  66.         elsif($G0X == 11){
  67.             ($pass[0],$pass[1]) = ($abso[0],$abso[1]);
  68.             
  69.             #î┤ò╢é≡âRâüâôâgÅoù═
  70.             outputOriginal($line[$i]);
  71.             
  72.             while(/([XY])([^A-Z\s]+)/g){
  73.                 $pass[$XYZ{$1}] = $2;
  74.             }
  75.         }
  76.         elsif($G0X == 11.1){
  77.             
  78.             #î┤ò╢é≡âRâüâôâgÅoù═
  79.             outputOriginal($line[$i]);
  80.             
  81.             ($destination[0],$destination[1]) = ($abso[0],$abso[1]);
  82.             while(/([XY])([^A-Z\s]+)/g){
  83.                 $destination[$XYZ{$1}] = $2;
  84.             }
  85.             
  86.             ($center[0],$center[1]) = getCenterFrom3P($abso[0],$abso[1],$pass[0],$pass[1],$destination[0],$destination[1]);
  87.             
  88.             if($center[0] eq "error"){
  89.                 print OUT "(-- ë~î╩òΓè╘é┼é½é▄é╣é± --)\n";
  90.             }
  91.             else{
  92.                 $word = getG02orG03($abso[0],$abso[1],$destination[0],$destination[1],
  93.                                             $pass[0],$pass[1],$center[0],$center[1]);
  94.                 
  95.                 $radius = getRadius($center[0]-$abso[0],$center[1]-$abso[1]);
  96.                 print OUT $word."X".$destination[0]."Y".$destination[1].
  97.                                 "R".$radius."\n";
  98.                 
  99.                 ($abso[0],$abso[1]) = ($destination[0],$destination[1]);
  100.             }
  101.         }
  102.         
  103.         else{
  104.             print OUT $line[$i];
  105.             while(/([XYZ])([^A-Z\s]+)/g){
  106.                 $abso[$XYZ{$1}] = $2;
  107.             }
  108.         }
  109.     }
  110. }
  111.  
  112. close(OUT);
  113.  
  114.  
  115. sub marume{
  116.     my ($num) = @_;
  117.  
  118.     if($coordinateNotation == 1){
  119.         return int($num+0.5);
  120.     }
  121.     else{
  122.         $num = int($num * 1000);
  123.         $num = $num / 1000;
  124.         if($num !~ /\./ and $num != 0){ $num = $num.'.'; }
  125.         return $num;
  126.     }
  127. }
  128.  
  129. sub getCenterFrom3P{
  130.     my ($x1,$y1,$x2,$y2,$x3,$y3) = @_;
  131.     my ($A,$B,$C,$D,$E,$xc,$yc);
  132.     
  133.     $E = ($x1-$x3)*($y1-$y2) - ($x1-$x2)*($y1-$y3);
  134.     if(abs($E) <= $EPSILON){
  135.         return "error";
  136.     }
  137.     else{
  138.         $A = ($y1-$y2)*($x1*$x1-$x3*$x3+$y1*$y1-$y3*$y3)/2;
  139.         $B = ($y1-$y3)*($x1*$x1-$x2*$x2+$y1*$y1-$y2*$y2)/2;
  140.         $C = ($x1-$x3)*($x1*$x1-$x2*$x2+$y1*$y1-$y2*$y2)/2;
  141.         $D = ($x1-$x2)*($x1*$x1-$x3*$x3+$y1*$y1-$y3*$y3)/2;
  142.         
  143.         $xc = ($A-$B)/$E;
  144.         $yc = ($C-$D)/$E;
  145.         return ($xc,$yc);
  146.     }
  147. }
  148.  
  149. sub getG02orG03{
  150.     my ($xs,$ys,$xe,$ye,$xp,$yp,$xc,$yc) = @_;
  151.     my ($As,$Ae,$Ap);
  152.     
  153.     $As = getAngle($xs-$xc,$ys-$yc);
  154.     $Ae = getAngle($xe-$xc,$ye-$yc);
  155.     $Ap = getAngle($xp-$xc,$yp-$yc);
  156.     
  157.     #CCWé⌐é╟éñé⌐é≡ö╗ÆΦé╖éΘ
  158.     #Æ╩ë▀ô_é¬èJÄnô_é╞ÅIù╣ô_é╠è╘é╠èpôxé╔éáéΩé╬üACCW
  159.     #èJÄnô_é¬ÅIù╣ô_éµéΦéαæσé½éóèpôxé╠é╞é½üACCWé╚éτé╬æµ4Å█î└é⌐éτæµ1Å█î└é╔ë±éΘé═é╕
  160.     if($Ae < $As){
  161.         #æµ4Å█î└é⌐éτæµ1Å█î└é╔ë±éΘé╞é½üAÅIù╣ô_é¬Æ╩ë▀ô_éµéΦéαæσé½éóèpôxé╚éτé╬üA
  162.         #Æ╩ë▀ô_é═æµ4Å█î└é⌐éτæµ1Å█î└é╔ë±é┴é╜îπé┼Æ╩ë▀é╖éΘé═é╕
  163.         if($Ap < $Ae){
  164.             $Ap += 2*pi;
  165.         }
  166.         $Ae += 2*pi;
  167.     }
  168.     
  169.     if($As < $Ap and $Ap < $Ae){
  170.         return "G03";
  171.     }
  172.     else{
  173.         return "G02";
  174.     }
  175. }
  176.  
  177. sub getAngle{
  178.     my ($x,$y) = @_;
  179.     my $theta = atan2($y,$x);
  180.     if($theta < 0){ $theta += 2*pi; }
  181.     return $theta;
  182. }
  183.  
  184.  
  185. sub getRadius{
  186.     my ($dx,$dy) = @_;
  187.     
  188.     return marume(sqrt($dx*$dx+$dy*$dy));
  189. }
  190.  
  191. sub divideComment{
  192.     my ($line) = @_;
  193.     my ($word, $newLine, $commentLine);
  194.     
  195.     $newLine = "";
  196.     $commentLine = "";
  197. #    while( $line =~ /([A-Z][^A-Z\s\(]+|\s*\(.*?\)\s*)/g ){
  198.     while( $line =~ /([A-Z][^A-Z\s\(]+|\s*\(.*?\))/g ){
  199.         $word = $&;
  200.         if($word =~ /\(/){
  201.             $commentLine = $commentLine . $word;
  202.         }
  203.         else{
  204.             $newLine = $newLine . $word;
  205.         }
  206.     }
  207.     
  208.     #if($commentLine eq ""){
  209.     #    return $commentLine;
  210.     #}
  211.     #else{
  212.         return ($newLine,$commentLine);
  213.     #}
  214. }
  215.  
  216. sub outputOriginal{
  217.     my ($line) = @_;
  218.     
  219.     $line =~ s/\s*$//;
  220.     print OUT "(-- " .$line. " --)\n";
  221. }
  222.